package cn.nfwx.tv.okhttp;

import android.os.Build;
import android.text.TextUtils;

import cn.nfwx.tv.BuildConfig;
import cn.nfwx.tv.base.NFWXApplication;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.LocationHelper;
import cn.nfwx.tv.helper.UmengPushHelper;
import cn.nfwx.tv.util.AESUtils;
import cn.nfwx.tv.util.AppUtil;
import cn.nfwx.tv.util.LogUtil;

/**
 * Created by dell on 2017/11/14.
 */

public class UrlHelper {


    public static final String IJK_ADD = "add";
    public static final String IJK_DEL = "del";

    /**
     * 在线播放拼接
     *
     * @return
     */
    public static String getLivePath(String path) {
        return Constants.base_video_url + path + ".json?type=live";
    }

    /**
     * 获取回看资源
     *
     * @param path
     * @return
     */
    public static String getVideoPath(String path) {
        return Constants.base_video_url + path + ".json?type=vod";
    }

    /**
     * 获取缩略图
     *
     * @param timeCode
     * @return
     */
    public static String getThumbUrl(String path, long timeCode) {
        return Constants.base_video_url + "/live/lvmgr1-live1/" + path + "/live.segments/" + timeCode + ".small.jpg";
    }

    /**
     * 获取栏目截图
     *
     * @return
     */
    public static String getColumnThumbUrl(String cid) {
        return Constants.base_video_url + "/query/" + cid + "?type=asset";
    }


    /**
     * 获取分享的视频的链接
     *
     * @param playVsid
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getShortVideoUrl(String playVsid, long startTime, long endTime) {
        StringBuilder builder = new StringBuilder(Constants.base_video_url + "/lclip/make.sjs?");
        builder.append("lid=").append(playVsid);
        builder.append("&start=").append(startTime);
        builder.append("&end=").append(endTime);
        return builder.toString();
    }

    /**
     * 获取分享的视频的链接
     *
     * @param gid
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getIjkShortVideoUrl(String gid, String startTime, String endTime) {
        StringBuilder builder = new StringBuilder(Constants.ijk_url + "api/clipget?");
        builder.append("gid=").append(gid);
        builder.append("&start=").append(startTime);
        builder.append("&end=").append(endTime);
        return builder.toString();
    }

    /**
     * 新的分享链接
     *
     * @return
     */
    public static String getNewShareUrl() {
        StringBuilder builder = new StringBuilder(Constants.ijk_url + "webtv/static/test.json?");
        return builder.toString();
    }


    /**
     * 获取下载视频的链接
     *
     * @param playVsid
     * @param startTime
     * @param endTime
     * @return
     */
    public static String getDownloadShortVideoUrl(String playVsid, long startTime, long endTime) {
        StringBuilder builder = new StringBuilder(Constants.base_video_url + "/lclip/make.sjs?");
        builder.append("lid=").append(playVsid);
        builder.append("&start=").append(startTime);
        builder.append("&end=").append(endTime);
        builder.append("&download=1");
        return builder.toString();
    }


    /**
     * 检索所有的相关节目，类似热门
     *
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     */
    public static String getColumnListUrl(String keyword, int pageNo, int pageSize) {
        StringBuilder builder = new StringBuilder(Constants.base_video_url + "/api?");
        builder.append("action=searchVideos");
        builder.append("&uid=").append(Constants.NFWX_UID);
        builder.append("&type=").append(2);
        builder.append("&priority=").append(0);
        builder.append("&keyword=").append(keyword);
        builder.append("&page_no=").append(pageNo);
        builder.append("&page_size=").append(pageSize);
        builder.append("&referer=").append(Constants.base_video_url);
        return builder.toString();
    }

    /**
     * 检索相关节目，类似
     *
     * @param keyword
     * @param pageNo
     * @param pageSize
     * @return
     */
    public static String getColumnDetailUrl(String keyword, int pageNo, int pageSize) {
        StringBuilder builder = new StringBuilder(Constants.base_video_url + "/api?");
        builder.append("action=searchVideos");
        builder.append("&uid=").append(Constants.NFWX_UID);
        builder.append("&type=").append(0);
        builder.append("&priority=").append(0);
        builder.append("&keyword=").append(keyword);
        builder.append("&page_no=").append(pageNo);
        builder.append("&page_size=").append(pageSize);
        builder.append("&referer=").append(Constants.base_video_url);
        return builder.toString();
    }

    /**
     * 获取栏目图标
     * "用query方式吧 /query/<assetsId>?type=asset";
     *
     * @param assests
     * @return
     */
    public static String getSubscriptionThumbUrl(String assests) {
        StringBuilder builder = new StringBuilder(Constants.base_video_url);
        builder.append("/query/");
        builder.append(assests);
        builder.append("?type=asset");
        return builder.toString();
    }

    /**
     * 添加搜索记录
     *
     * @return
     */
    public static String getAddSearchUrl() {
        StringBuilder builder = new StringBuilder(Constants.test_url);
        builder.append("/nfwx-hot/v1/hot");
        return builder.toString();
    }

    /**
     * 获取热门搜索
     *
     * @param count
     * @return
     */
    public static String getHotKeywordUrl(int count) {
        StringBuilder builder = new StringBuilder(Constants.test_url);
        builder.append("/nfwx-hot/v1/hot");
        builder.append("?count=").append(count);
        builder.append("&type=").append("month");
        return builder.toString();
    }

    /**
     * 统计的服务器提交地址
     *
     * @return
     */
    public static String getGrabUrl() {
        StringBuilder builder = new StringBuilder(Constants.test_url);
        builder.append("/nfwx-stat/v1/grab");
        return builder.toString();
    }

    /**
     * 获取公交直播地址
     *
     * @return
     */
    public static String getQpopProgramListUrl() {
        StringBuffer buffer = new StringBuffer(Constants.Qpop_url);
        buffer.append("/playlistV2");
        return buffer.toString();
    }

    /**
     * http://192.168.0.1/hk/hklist?id=86&d=2018-03-20&pwd=testpwd
     *
     * @param lid
     * @param dateStr
     * @return
     */
    public static String getQpopVodListUrl(int lid, String dateStr) {
        StringBuffer buffer = new StringBuffer(Constants.Qpop_url);
        buffer.append("/hk/hklist?");
        buffer.append("id=").append(lid);
        buffer.append("&d=").append(dateStr);
        buffer.append("&pwd=").append("testpwd");
        return buffer.toString();
    }

    /**
     * 进入公交环境调用
     *
     * @param uid
     * @return
     */
    public static String getQpopBusUrl(int uid) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/bus?");
        buffer.append("uid=").append(uid);
        return buffer.toString();
    }

    /**
     * http://192.9.101.250:8080/api/queryads
     * 广告图
     *
     * @return
     */
    public static String getAdvertiseUrl() {
        StringBuffer buffer = new StringBuffer("http://m3.southtv.cn:9180/api/queryads");
        return buffer.toString();
    }

    /**
     * 热修复
     *
     * @return
     */
    public static String getHeatRegeneration() {
        StringBuffer buffer = new StringBuffer("");
        return buffer.toString();
    }

    /**
     * 获取直播url
     *
     * @param uid
     * @return
     */
    public static String getLivesUrl(int uid) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url_live + "api/querylive?");
        if (TextUtils.isEmpty(LocationHelper.getInstance().getRegion())) {
            return buffer.toString();
        }
        buffer.append("region=").append(LocationHelper.getInstance().getRegion());
        buffer.append("&uid=").append(uid);
        return buffer.toString();
    }

    /**
     * 获取回看列表
     *
     * @param gid
     * @return
     */
    public static String getReviewUrl(String gid, String day) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url_live + "api/querypvr?");
        buffer.append("gid=").append(gid);
        if (!TextUtils.isEmpty(day)) {
            buffer.append("day=").append(day);
        }
        return buffer.toString();
    }

    /**
     * 获取时移地址
     *
     * @param gid
     * @param timeShift
     * @return
     */
    public static String getTimesShiftUrl(String gid, long timeShift) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/timeshift?");
        buffer.append("gid=").append(gid);
        buffer.append("&uuid=").append(System.currentTimeMillis());
        buffer.append("&start=").append(timeShift);
        return buffer.toString();
    }

    /**
     * 获取帧图
     *
     * @param gid
     * @return
     */
    public static String getClipsnapUrl(String gid) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/clipsnap?");
        buffer.append("gid=").append(gid);
        return buffer.toString();
    }

    /**
     * 广告
     *
     * @return
     */
    public static String getIjkAdvertiseUrl() {
        StringBuffer buffer;
        if (BuildConfig.IS_RELEASE) {
            buffer = new StringBuffer(Constants.ijk_url + "api/queryads");
        } else {
            buffer = new StringBuffer("http://yxb.southtv.cn:9180/api/queryads_debug");
        }
        return buffer.toString();
    }

    /**
     * 获取热门视频的链接
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public static String getIjkHotVideosUrl(int pageNo, int pageSize) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/hotvideos?");
        buffer.append("start=").append((pageNo - 1) * pageSize);
        buffer.append("&size=").append(pageSize);
        return buffer.toString();
    }

    /**
     * 查询热门关键字
     *
     * @return
     */
    public static String getIjkKeywordsUrl() {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/keywords");
        return buffer.toString();
    }

    /**
     * 搜索关键字
     *
     * @param keywork
     * @return
     */
    public static String getIjkSearchResultUrl(String keywork) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/search?");
        buffer.append("keyword=").append(keywork);
        return buffer.toString();
    }

    /**
     * 获取热门订阅
     *
     * @return
     */
    public static String getIjkHotSubscriptUrl() {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/subject");
        return buffer.toString();
    }

    /**
     * 获取我的订阅
     *
     * @param uid
     * @return
     */
    public static String getIjkMySubscriptUrl(int uid) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/querysubscribed?");
        buffer.append("uid=").append(uid);
        return buffer.toString();
    }

    /**
     * 增加删除订阅
     *
     * @param subject_id
     * @param uid
     * @param action
     * @return
     */
    public static String getIjkAddDelSubscriptUrl(int subject_id, int uid, String action) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/subscribe?");
        buffer.append("subject_id=").append(subject_id);
        buffer.append("&uid=").append(uid);
        buffer.append("&action=").append(action);
        return buffer.toString();
    }

    /**
     * 获取预约的Url
     *
     * @return
     */
    public static String getIjkAppointUrl(String deviceid) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/queryreserved?");
        buffer.append("uid=").append(deviceid);
        return buffer.toString();
    }

    /**
     * 添加或者删除预约
     *
     * @param deviceId
     * @param gid
     * @param program_id
     * @param action
     * @return
     */
    public static String getIjkAddDelAppintUrl(String deviceId, String gid, String program_id, String action) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/reserve?");
        buffer.append("uid=").append(deviceId);
        buffer.append("&gid=").append(gid);
        buffer.append("&program_id=").append(program_id);
        buffer.append("&action=").append(action);
        return buffer.toString();
    }

    /**
     * 获取观看记录id
     *
     * @return
     */
    public static String getIjkViewHistroyUrl(int uid) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/viewhistory?");
        buffer.append("uid=").append(uid);
        return buffer.toString();
    }

    /**
     * 增加观看记录
     *
     * @param uid
     * @param program_id
     * @param position
     * @param percent
     * @return
     */
    public static String getAddViewHistroyUrl(int uid, int program_id, long position, int percent) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/setviewhistory?");
        buffer.append("uid=").append(uid);
        buffer.append("&program_id=").append(program_id);
        buffer.append("&position=").append(position);
        buffer.append("&percent=").append(percent);
        return buffer.toString();
    }

    /**
     * 删除观看记录
     *
     * @param uid
     * @param programids
     * @return
     */
    public static String getDelViewHistroyUrl(int uid, String programids) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/delviewhistory?");
        buffer.append("uid=").append(uid);
        buffer.append("&program_id=").append(programids);
        return buffer.toString();
    }

    /**
     * 获取直播加密地址
     *
     * @param path
     * @return
     */
    public static String getIjkLivePath(String path) {
        //String s = AESUtils.encrypt(Constants.key, System.currentTimeMillis() / 1000L + "");
        if (path.contains("http")) {
            long time = System.currentTimeMillis() / 1000L;
            String sign = AppUtil.getSign(path, time);
            StringBuffer buffer = new StringBuffer(path);
            if (path.contains("?")) {
                if (path.contains("&")) {
                    buffer.append("&uid=").append(AccountHelper.getInstance().getUid(NFWXApplication.getInstance()));
                } else {
                    buffer.append("uid=").append(AccountHelper.getInstance().getUid(NFWXApplication.getInstance()));
                }
                buffer.append("&t=").append(time);
                buffer.append("&ip=").append(AccountHelper.getInstance().getIp());
                buffer.append("&sign=").append(sign);
                //buffer.append("&token=").append(s);
            } else {
                buffer.append("?uid=").append(AccountHelper.getInstance().getUid(NFWXApplication.getInstance()));
                buffer.append("&t=").append(time);
                buffer.append("&ip=").append(AccountHelper.getInstance().getIp());
                buffer.append("&sign=").append(sign);
                //buffer.append("&token=").append(s);
            }
            return buffer.toString();
        }
        return path;
    }

    /**
     * 新系统登录
     *
     * @return
     */
    public static String getIjkLoginUrl() {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/login");
        return buffer.toString();
    }

    /**
     * 检查手机
     *
     * @return
     */
    public static String getIjkCheckPhoneUrl(String phone) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/mexist?");
        buffer.append("mobile=").append(phone);
        return buffer.toString();
    }

    /**
     * 发送验证码
     *
     * @param phone
     * @return
     */
    public static String getIjkSmsCodeUrl(String phone) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/sendtext?");
        buffer.append("mobile=").append(phone);
        return buffer.toString();
    }

    /**
     * 注册
     *
     * @return
     */
    public static String getIjkRegisterUrl() {
        return Constants.ijk_url + "api/register";
    }

    /**
     * 修改密码
     *
     * @param phone
     * @param pwd
     * @return
     */
    public static String getIjkResetPwd(String phone, String pwd, String code) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/changepw?");
        buffer.append("mobile=").append(phone);
        buffer.append("&password=").append(pwd);
        buffer.append("&code=").append(code);
        return buffer.toString();
    }

    /**
     * 检查微信是否有注册
     *
     * @param code
     * @return
     */
    public static String getIjkCheckWxUrl(String code) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/exist?");
        buffer.append("wxcode=").append(code);
        return buffer.toString();
    }

    /**
     * 获取绑定链接
     *
     * @return
     */
    public static String getIjkBindPhoneUrl() {
        return Constants.ijk_url + "api/bind";
    }

    /**
     * 检查验证码
     *
     * @param phone
     * @param sms
     * @return
     */
    public static String getIjkCheckSmsUrl(String phone, String sms) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/checkcd?");
        buffer.append("mobile=").append(phone);
        buffer.append("&code=").append(sms);
        return buffer.toString();
    }

    /**
     * 获取验证码图片
     *
     * @param mobileNo
     * @return
     */
    public static String getSmsCheckImgUrl(String mobileNo) {
        StringBuffer buffer = new StringBuffer(Constants.ijk_url + "api/gencode?");
        buffer.append("mobile=").append(mobileNo);
        buffer.append("&timeCode=").append(System.currentTimeMillis());
        return buffer.toString();
    }

    /**
     * 应用更新信息
     *
     * @return
     */
    public static String getIjkUpdateUrl() {
        return Constants.ijk_url + "api/version";
    }

    /**
     * 获取精彩瞬间
     *
     * @return
     */
    public static String getIjkMovementUrl() {
        return Constants.ijk_url + "api/movement";
    }

    /**
     * 修改昵称
     *
     * @return
     */
    public static String getIjkchangeNickUrl(String phone, String nick) {
        StringBuffer stringBuffer = new StringBuffer(Constants.ijk_url).append("api/changenk?");
        stringBuffer.append("mobile=").append(phone);
        stringBuffer.append("&nick=").append(nick);
        return stringBuffer.toString();
    }

    /**
     * 认证
     *
     * @return
     */
    public static String getBusAuthUrl(String phone, String token) {
        StringBuffer stringBuffer = new StringBuffer(Constants.bus_url).append("auth?");
        stringBuffer.append("username=").append(phone);
        stringBuffer.append("&token=").append(token);
        return stringBuffer.toString();
    }

    /**
     * @param token
     * @param type
     * @param mac
     * @return
     */
    public static String getRefreshAuthUrl(String token, String type, String mac) {
        StringBuffer stringBuffer = new StringBuffer(Constants.bus_url).append("wifiabc/appauth?");
        stringBuffer.append("token=").append(token);
        stringBuffer.append("&type=").append(type);
        stringBuffer.append("&umac=").append(mac);
        return stringBuffer.toString();
    }

    /**
     * APP用户协议
     *
     * @return
     */
    public static String getAppAgreement() {
        StringBuffer stringBuffer = new StringBuffer(Constants.ijk_url_live).append("link/agreement/app.html");
        return stringBuffer.toString();
    }

    /**
     * 获取app地址
     *
     * @return
     */
    public static String getAppLink() {
        StringBuffer stringBuffer = new StringBuffer(Constants.ijk_url_live).append("link/share/applink.html");
        return stringBuffer.toString();
    }

    /**
     * 获取App二维码
     *
     * @return
     */
    public static String getAppqrcode() {
        StringBuffer stringBuffer = new StringBuffer(Constants.ijk_url_live).append("link/share/appqrcode.html");
        return stringBuffer.toString();
    }

    /**
     * @return
     */
    public static String getShareThumbUrl() {
        return "http://vodcdn.southtv.cn/webtv/static/videoshare.jpg";
    }

    /**
     * 节目分享链接
     *
     * @param gid
     * @param filename
     * @return
     */
    public static String getShareVideoUrl(String gid, String filename) {
        StringBuffer stringBuffer = new StringBuffer("http://vodcdn.southtv.cn/webtv/index.html#/share?");
        stringBuffer.append("channel=").append(gid);
        stringBuffer.append("&epgname=").append(filename);
        return stringBuffer.toString();
    }

    /**
     * 获取IP地址url
     *
     * @return
     */
    public static String getIpUrl() {
        StringBuffer stringBuffer = new StringBuffer(Constants.ijk_url).append("myip?");
        return stringBuffer.toString();
    }

    /**
     * 获取企业频道
     *
     * @return
     */
    public static String getCompanyVideoUrl(String mobile) {
        StringBuffer stringBuffer = new StringBuffer(Constants.ijk_url).append("api/company?");
        stringBuffer.append("mobile=").append(mobile);
        return stringBuffer.toString();
    }
}
